import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;

import suit.ui.Application;
import suit.ui.Box;
import suit.ui.Button;
import suit.ui.Label;
import suit.ui.Scale;
import suit.ui.Screen;
import suit.ui.themes.DefaultTheme;

public class TonePlayer extends Application {

	Label status = new Label("Loaded");

	Scale note;
	Scale velocity;

	public TonePlayer() {

		Screen main = new Screen(new DefaultTheme());

		Box vbox = new Box(Box.Vert, true, 6);
		vbox.setBorderWidth(6);

		vbox.pack(status, true, true, 0);

		// Note
		Label n = new Label("Note:");
		n.setAlign(Label.BOTTOM | Label.LEFT);
		vbox.pack(n, true, true, 0);

		note = new Scale(Scale.Horiz, 57, 93, 1, 60);
		vbox.pack(note, true, true, 0);

		// Velocity
		Label v = new Label("Velocity:");
		v.setAlign(Label.BOTTOM | Label.LEFT);
		vbox.pack(v, true, true, 0);

		velocity = new Scale(Scale.Horiz, 0, 100, 1, 100);
		vbox.pack(velocity, true, true, 0);

		// Send
		Button send = new Button("Play");
		send.connect(new Button.Pressed() {

			public void onPressed(Button source) {
				int du = 10000;
				int no = (int) note.getValue();
				int ve = (int) velocity.getValue();
				status.setText("Playing note " + no + " for " + du
						+ " milliseconds with volume of " + ve);
				try {
					Manager.playTone(no, du, ve);
				} catch (MediaException e) {
					status.setText(e.getMessage());
				}

			}
		});
		send.connect(new Button.Released() {

			public void onReleased(Button source) {
				int du = 1;
				int no = (int) note.getValue();
				int ve = (int) velocity.getValue();
				try {
					Manager.playTone(no, du, ve);
				} catch (MediaException e) {
					status.setText(e.getMessage());
				}

			}
		});

		vbox.pack(send, true, true, 0);

		main.add(vbox);
		setScreen(main);

	}

	public boolean onKeyPressed(int key) {
		if (key == -8)
			notifyDestroyed();
		return false;
	}
}
